﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Clients_Control_sanpham : System.Web.UI.UserControl
{
    private string cssTab = "ui-state-default ui-corner-top ui-tabs-selected ui-state-active";
    private string cssTabHide = "ui-state-default ui-corner-top";
    private string cssDiv = "ui-tabs-panel ui-widget-content ui-corner-top";
    private string cssDivHide = "ui-tabs-panel ui-widget-content ui-corner-top ui-tabs-hide";
    private String strLiDiv = "<li> <a href='" + Constant.ROOT_PATH + "$link'>$text</a></li>";
    protected string liTag = string.Empty;
    protected string divTag = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack) {
            LoadDetailProduct();
            ProductOther();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {

            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }
    }

    private void ProductOther() {
        String url = "";
        try {
            int id = int.Parse(Request.Params["id"].ToString());
            using (DamiWebDataContext db = new DamiWebDataContext())
            {
                var query = from p in db.tbl_Product_Langs
                            where p.idLang == 1 && p.idProduct != id
                            orderby p.tbl_Product.orderId ascending
                            select p;
                foreach (var c in query) {
                    try
                    {
                        if (!string.IsNullOrEmpty(c.nameProduct.ToString()))
                        {
                            String str = String.Format("{0}/{1}/{2}",Constant.strSanpham ,c.idProduct.ToString(),Constant.ChangeString(c.nameProduct.ToString()));
                            url = url + strLiDiv.Replace("$text", c.nameProduct).Replace("$link", str);
                        }
                    }
                    catch { }
                  
                }
            }
        }
        catch { }

        ltlSanpham.Text = url;
        if (string.IsNullOrEmpty(url))
        {
            lbl.Visible = false;
        }
        else
        {
            lbl.Visible = true;
        }
    }

    private void LoadDetailProduct() {
        WebData data = new WebData();
        try {

            int id = -1;
            try {
                id = int.Parse(Request.Params["id"].ToString());
            }
            catch { 

            }

            if (id == -1)
            {
               DataLibrary.tbl_Product product = data.tbl_Products.GetAll()[0];
               id = product.IdProduct;
            }

            List<DataLibrary.tbl_DetailProduct> list = data.tbl_DetailProducts.GetAll().FindAll(p => p.IdProduct == id 
                && p.IsActive == 1);

            String strSql = "select p.imageProduct,lang.* from tbl_Product_Lang lang,tbl_Product p"
                + " where p.idProduct=lang.idProduct and lang.idLang=1 and p.isActive=1 and p.idProduct=@idProduct";

            DataTable tb =data.tbl_Product_Langs.GetDataTable(strSql,CommandType.Text,
                new SqlParameter("@idProduct", id));

            String strImage = "<img class='img_product'  src='"+Constant.ROOT_PATH+"$src'>";
            if (tb.Rows.Count > 0)
            {
                String image = tb.Rows[0]["imageProduct"].ToString();
                lblImage.Text = strImage.Replace("$src",image);
                lblTitle.Text = tb.Rows[0]["nameProduct"].ToString();
                lblNoidung.Text = tb.Rows[0]["introProduct"].ToString();
                lblMota.Text = tb.Rows[0]["Description"].ToString();
            }

            //this.grdDeteilProduct.DataSource = tb;
           // this.grdDeteilProduct.DataBind();
                    for (int j = 0; j < list.Count; j++)
                    {
                        if (j == ActiveIndexTab)
                        {
                            liTag += BuildLiTag(list[j].Name, j, cssTab);
                            divTag += BuildDivTag(list[j].Description, j, cssDiv);
                        }
                        else
                        {
                            liTag += BuildLiTag(list[j].Name, j, cssTabHide);
                            divTag += BuildDivTag(list[j].Description, j, cssDivHide);
                        }
                    }
                   
                    Literal1.Text = liTag;
                    Literal2.Text = divTag;

             
        }
        catch { }
        
    }
    private string BuildLiTag(string content, int index, string css)
    {
        return "<li class=\"" + css + "\"><a href=\"#" + index + "\" title='tabs-" + index + "' class='contentTab'>" + content + "</a></li>";
    }

    private string BuildDivTag(string content, int index, string css)
    {
        return "<div id=\"tabs-" + index + "\" class=\"" + css + "\">" + content + "</div>";
    }

    public int ActiveIndexTab
    {
        set;
        get;
    }
}

